package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.mapper.OrderMapper;
import com.example.mapper.WriteOffMapper;
import com.example.pojo.entity.Order;
import com.example.pojo.entity.WriteOff;
import com.example.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    WriteOffMapper WriteOffMapper;
    @Resource
    OrderMapper orderMapper;

    @Override
    public void insert(Order order) {
        order.setOrderNum(IdUtil.getSnowflakeNextIdStr());
        order.setOrderState(false);
        order.setCreateTime(DateUtil.now());
        if (order.getType() == null){
            order.setType(1);
        }else {
            // 添加票务订单到核销库
            WriteOff writeOff = new WriteOff();
            writeOff.setOrderNum(order.getOrderNum());
            writeOff.setState(false);
            WriteOffMapper.insert(writeOff);
        }
        orderMapper.insert(order);

    }

    @Override
    public void updateOrder(Order order) {

        orderMapper.updateOrder(order);
    }

    @Override
    public void removeById(Integer id) {

        orderMapper.removeById(id);
    }

    @Override
    public void removeBatchByIds(List<Integer> ids) {

        for (Integer id : ids){
            orderMapper.removeById(id);
        }
    }

    @Override
    public List<Order> list() {
        List<Order> list = orderMapper.list();
        return list;
    }

    @Override
    public Order getById(Integer id) {
        Order order = orderMapper.getById(id);
        return order;
    }

    @Override
    public PageInfo<Order> selectByPage(Integer pageNum, Integer pageSize, String goodsName, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);

        List<Order> list = orderMapper.selectByPage(goodsName,userId);
        return new PageInfo<>(list);
    }

    @Override
    public Order getByOrderNum(String orderNum) {
        Order order = orderMapper.getByOrderNum(orderNum);
        return order;
    }
}
